home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / Append-c / Append.µ.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-04  |  3.5 KB  |  153 lines  |  [TEXT/MMCC]

  1. //• append.c -- by Mark^Zimmermann, 19871019-....
  2.  
  3. //• This little effort lets you append chosen text files together ...
  4. //• pick the first file, and then subsequent choices from the
  5. //• StdFiles dialog are tacked onto the end of it ... designed
  6. //• to help save time and space relative to DivJoin, etc. programs
  7. //• which copy all files as they are joined together.
  8.  
  9. //• Written in Lightspeed C ... 
  10. //• Please send improvements and suggestions to me:
  11. //• science(at)NEMS.ARPA;
  12. //• [75066,2044] on CompuServe;
  13. //• Mark^Zimmermann, 9511 Gwyndale Dr., Silver Spring, MD  20910, USA;
  14. //• telephone 301-565-2166.
  15.  
  16. //• Thank you!  ^z
  17.  
  18. //• prototypes for my functions ... I do love prototypes!
  19.  
  20. //#include <StandardFile.h>
  21.  
  22. void    main(void);
  23. void    Append_File(short refNum0, Str255 *fnXp, short vRefX);
  24. int        GetFileZ(Str255 *fnp, short *vRefp);
  25. void    pStrCopy(char *p1, char *p2);
  26. void    giveUp(void);
  27.  
  28. //• set up buffer of size 25,600 bytes (= 512 * 50) for copying through;
  29. //• ZBUFSIZ must be an integer, < 32768
  30.  
  31. #define ZBUFSIZ  25600
  32.  
  33. char buf[ZBUFSIZ];
  34.  
  35. //• set up a little window to give info to the user...very static
  36.  
  37. WindowRecord w_record;
  38. WindowPtr info_window;
  39. Str255 string;
  40.  
  41. //• main routine to initialize everything under the sun (is all this
  42. //• really necessary?) and then get the names of the files to join ... if
  43. //• they open ok, start joining them, and quit when the user chooses
  44. //• "Cancel"....
  45.  
  46. void main() 
  47. {
  48.     Str255 fn0, fnX;
  49.     short vRef0, vRefX, refNum0, i;
  50.  
  51.     InitGraf (&qd.thePort);
  52.     InitFonts ();
  53.     FlushEvents (everyEvent, 0);
  54.     InitWindows ();
  55.     InitMenus ();
  56.     TEInit ();
  57.     InitDialogs (0L);
  58.     InitCursor ();
  59.     MaxApplZone ();
  60.     
  61.     info_window = GetNewWindow (128, &w_record, (WindowPtr) - 1L);
  62.     ShowWindow (info_window);
  63.     SetPort (info_window);
  64.     TextFont (0);
  65.     for (i = 1; i <= 3; ++i)
  66.     {
  67.         MoveTo (4, 15 * i - 5);
  68.         GetIndString ((StringPtr) string, 128, i);
  69.         DrawString ((StringPtr) string);
  70.     }
  71.  
  72.     if (GetFileZ (&fn0, &vRef0))
  73.     {
  74.         if (FSOpen ((StringPtr) fn0, vRef0, &refNum0) != noErr)
  75.             giveUp ();
  76.         while (GetFileZ (&fnX, &vRefX))
  77.             Append_File (refNum0, &fnX, vRefX);
  78.         FSClose (refNum0);
  79.     }
  80. }
  81.  
  82. //• Routine to append the contents of file X to file 0.
  83.  
  84. void Append_File(short refNum0, Str255 *fnXp, short vRefX)
  85. {
  86.     short refNumX, err;
  87.     long count;
  88.     
  89.     if (FSOpen ((StringPtr) fnXp, vRefX, &refNumX) != noErr)
  90.         giveUp ();
  91.     if (SetFPos (refNumX, fsFromStart, 0L) != noErr)
  92.         giveUp ();
  93.     if (SetFPos (refNum0, fsFromLEOF, 0L) != noErr)
  94.         giveUp ();
  95.     for (;;)
  96.     {
  97.         count = ZBUFSIZ;
  98.         err = FSRead (refNumX, &count, buf);
  99.         if (err != noErr && err != eofErr)
  100.             giveUp ();
  101.         if (count == 0)
  102.             break;
  103.         if (FSWrite (refNum0, &count, buf) != noErr)
  104.             giveUp ();
  105.     }
  106.     FSClose (refNumX);
  107.     return;
  108. }
  109.  
  110. //• following variables and routine do the standard files dialog
  111. //• to get the name of the file use ... cribbed from the MiniEdit
  112. //• example that comes with LSC....
  113.  
  114. static Point SFGwhere = { 90, 82 };
  115. static SFReply reply;
  116.  
  117. int GetFileZ (Str255 *fnp, short *vRefp)
  118. {
  119.     SFTypeList myTypes;
  120.  
  121.     myTypes[0]='TEXT';
  122.     SFGetFile( SFGwhere, "\p", 0L, 1, myTypes, 0L, &reply);
  123.     if (reply.good)
  124.     {
  125.         pStrCopy( (char *)reply.fName, (char *)fnp);
  126.         *vRefp = reply.vRefNum;
  127.         return(1);
  128.     }
  129.     else return(0);
  130. }
  131.  
  132. //• routine to copy a pascal string from one place to another.... used in
  133. //• above Standard Files routine....
  134.  
  135. void pStrCopy(char *p1, char *p2)
  136. {
  137.     short len;
  138.     
  139.     len = *p2++ = *p1++;
  140.     while (--len >= 0)
  141.         *p2++ = *p1++;
  142.     return;
  143. }
  144.  
  145. //• routine to give up with a beep if an error occurs during opening the
  146. //• file or fixing it....
  147.  
  148. void giveUp ()
  149. {
  150.     SysBeep (10);
  151.     ExitToShell ();
  152. }
  153.